home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / os2 / sidis100.zip / USERCTRL / USERCTRL.CMD < prev    next >
OS/2 REXX Batch file  |  1996-05-25  |  26KB  |  900 lines

  1. /*************************************************************************/
  2. /*                                                                       */
  3. /*  REXX Control Script for Modem Handling and User Access Control       */
  4. /*                                                                       */
  5. /* (C) 1995, 1996   Axel Mueller (amueller@stargate.rz.fh-offenburg.de)  */
  6. /*     Version 1.0  Last modified: 05/24/96                              */
  7. /*************************************************************************/
  8.  
  9. /* ##########################*/
  10. /* ModemIndex = 1 means COM1 */   
  11. /* ModemIndex = 2 means COM2 */
  12. /* ModemIndex = 3 means COM3 */
  13. /* ModemIndex = 4 means COM4 */
  14. /* ##########################*/
  15.  
  16. parse arg ModemIndex
  17.  
  18. call on error
  19.  
  20.  
  21. /************************************************************************/
  22. /*                                                                      */
  23. /* Setup                                                                */
  24. /*                                                                      */
  25. /************************************************************************/
  26.  
  27. /* boolean variables */
  28. True            = 1
  29. False        = 0
  30.  
  31.  
  32. /* general */
  33. ModemNumber    = value('MODEMNUMBER',,'OS2ENVIRONMENT')
  34.  
  35.  
  36. /**************/
  37. /* Path setup */
  38. /**************/
  39.  
  40. /* Root directory: c:\userctrl */
  41. Path            = 'c:\userctrl'
  42.  
  43. /* Path to log files */
  44. LogPath            = value('ONLINELOGDIR',,'OS2ENVIRONMENT')
  45.  
  46.  
  47. /* Directory under which all the POP mail directories are situated */
  48. MailPath        = value('MAILDIR',,'OS2ENVIRONMENT')
  49.  
  50. /* Path to IBMs TCP/IP ETC directory */
  51. ETCPath        = value('ETC',,'OS2ENVIRONMENT')
  52.  
  53.  
  54. /* ##### Log files ##### */
  55.  
  56. /* Log file for all messages generated by this REXX script */
  57. SystemLog    = LogPath'\UserCtl'ModemIndex'.log'
  58.  
  59.  
  60. /* ##### Files needed for login procedure ##### */
  61.  
  62. /* File to be send to modem user after CONNECT but before login */
  63. WelcomeFile    = Path'\welcome.msg'
  64.  
  65.  
  66. /* ##### TCP/IP related ##### */
  67.  
  68. /* Configuration file for PPP driver of IBM's TCP/IP */
  69. PPP_CFGFile    = ETCPath'\ppp'ModemIndex'.cfg'
  70.  
  71.  
  72. /******************/
  73. /* COM port setup */
  74. /******************/
  75.  
  76.  
  77. PortName        = 'com'ModemIndex
  78. PortHandle    = ''
  79.  
  80. WriteLim        = 50
  81. ReadLim        = 50                     /* 0,5 seconds timout */
  82. Flags1        = '00001001'
  83. Flags2        = '10100000'
  84. Flags3        = '11010010'
  85. ErrChar        = '00'
  86. BrkChar        = '00'
  87. XonChar        = '11'
  88. XoffChar        = '13'
  89. EnhParms        = '00000010'
  90.  
  91. crlf            = D2C(13)''D2C(10)
  92. none_on        = '00'
  93. dtr_on        = '01'
  94. rts_on        = '02'
  95. both_on        = '03'
  96. none_off        = 'FF'
  97. dtr_off        = 'FE'
  98. rts_off        = 'FD'
  99. both_off        = 'FC'
  100.  
  101. Timeout        = 15                     /* Wait 15 seconds for com port */
  102. UserMaxLen    = 25
  103. PwdMaxLen    = 25
  104.  
  105.  
  106. '@echo off'
  107. 'cd' Path
  108.  
  109. call flagfile('START')
  110.  
  111.  
  112.  
  113. /************************************************************************/
  114. /*********************                ***********************************/
  115. /********************* Main function  ***********************************/
  116. /*********************                ***********************************/
  117. /************************************************************************/
  118.  
  119.  
  120. do forever
  121.  
  122.  
  123.     /* open COM port */
  124.     call open_com_port
  125.  
  126.     /* set modified Device Control Block (dcb) */
  127.     rc = RxIOCtlSetDcbParm( PortHandle, WriteLim, ReadLim, Flags1, Flags2, Flags3, ErrChar, BrkChar, XonChar, XoffChar )
  128.     if rc <> 0 then
  129.         call log('MAINFUNCT : ###ERROR### RxIOCtlSetDcbParm failed with rc='rc)
  130.     else
  131.         call log('MAINFUNCT : modified Device Control Block set')
  132.  
  133.  
  134.     /* set Enhanced Parameters */
  135.     rc = RxIOCtlSetEnhParm( PortHandle, EnhParms )
  136.     if rc <> 0 then
  137.         call log('MAINFUNCT : ###ERROR### RxIOCtlSetEnhParm failed with rc='rc)
  138.     else
  139.         call log('MAINFUNCT : Enhanced parameter set')
  140.  
  141.  
  142.     /* initialize modem */
  143.     call modem_init
  144.    
  145.     /* create flag file indicating that the system is waiting for caller */
  146.     call flagfile('WAIT')
  147.    
  148.     /* wait for input from buffer; RING will be assumed */
  149.     Buffer = ""
  150.     Ring = False
  151.     call log('MAINFUNCT : waiting for RING ...' )
  152.     rc = lineout(SystemLog)
  153.     rc = RxIOCtlRead(PortHandle, 0, -1, 'Buffer')
  154.     if rc <> 0 then
  155.         call log('MAINFUNCT : ###ERROR### RxIOCtlRead failed with rc='rc)
  156.     BufferEmpty = True
  157.     do while BufferEmpty
  158.         if length(Buffer) > 3 then
  159.             BufferEmpty = False
  160.         else
  161.             do
  162.                 Buffer = ''
  163.                 rc = RxIOCtlRead(PortHandle, 0, 1, 'Buffer')
  164.                 if rc <> 0 then
  165.                     call log('MAINFUNCT : ###ERROR### RxIOCtlRead failed with rc='rc)
  166.             end
  167.     end
  168.     Buffer = strip(Buffer,,D2C(10))
  169.     Buffer = strip(Buffer,,D2C(13))
  170.     call log('MAINFUNCT : 'Buffer )
  171.  
  172.     /* create flag file indicating that login is in process */
  173.     call flagfile('LOGIN')
  174.  
  175.     if Buffer == 'RING' then
  176.         do               /* pick up the line */
  177.             Rem = 0
  178.             rc = RxIOCtlWrite( PortHandle, 'ATA'||D2C(13), 'Rem' )
  179.             if rc <> 0 then
  180.                 call log('MAINFUNCT : ###ERROR### RxIOCtlWrite (ATA) failed with rc='rc)
  181.        
  182.             /* wait for CONNECT or NO CARRIER; Timeout = 70 seconds */
  183.             Connect = False
  184.             Cycle = 0
  185.             MaxCycle = 70 * 100 / ReadLim
  186.             do while (Connect == False) & (Cycle < MaxCycle)
  187.                 Cycle = Cycle + 1
  188.                 rc = RxIOCtlRead( PortHandle, 0, 1, 'Buffer' )
  189.                 if length(Buffer) > 3 then
  190.                     do
  191.                         Buffer = strip(Buffer,,D2C(10))
  192.                         Buffer = strip(Buffer,,D2C(13))
  193.                         if left(Buffer, 7) == "CONNECT" then
  194.                                 Connect = True
  195.                         if Buffer == "NO CARRIER" then
  196.                                 Cycle = MaxCycle
  197.                     end
  198.             end
  199.             call log('MAINFUNCT : 'Buffer)
  200.  
  201.             call check_carrier    
  202.             if Carrier then
  203.                 call handle_login           /* start login procedure */
  204.             else
  205.                 call log('MAINFUNCT : !!WARNING!! Timeout waiting for CONNECT')
  206.  
  207.         end
  208.  
  209.     /* close COM-Port */
  210.     call close_com_port
  211.  
  212.     /* wait 15 seconds to force line down */
  213.     call log('MAINFUNCT : Waiting for 15 seconds ...')
  214.     call SysSleep 15
  215.  
  216. end
  217.  
  218. /* exit programm */
  219. exit
  220.  
  221.  
  222.  
  223.  
  224. /************************************************************************/
  225. /*********************                ***********************************/
  226. /*********************   Functions    ***********************************/
  227. /*********************                ***********************************/
  228. /************************************************************************/
  229.  
  230.  
  231.  
  232. /************************************************************************/
  233. /* handle_login()                                                       */
  234. /*                                                                      */
  235. /* main fuction handling the login procedure                            */
  236. /************************************************************************/
  237.  
  238. handle_login:
  239.           
  240.     call check_carrier    
  241.     if Carrier then
  242.         call welcome_screen                    /* send welcome screen */
  243.  
  244.     call check_carrier
  245.     LoginOkay = False
  246.     Attempt = 1
  247.     do while (LoginOkay = False) & (Attempt<=3) & Carrier
  248.  
  249.         call log('HNDLELOGIN: --- 'Attempt'. Attempt ---' )
  250.  
  251.         call check_carrier
  252.         if Carrier then
  253.             User = get_user()
  254.         else
  255.             call log( 'HNDLELOGIN: !!WARNING!! Carrier lost. Skipping user request' )
  256.  
  257.         call check_carrier
  258.         if Carrier then
  259.             Password = get_password()
  260.         else
  261.             call log( 'HNDLELOGIN: !!WARNING!! Carrier lost. Skipping password request' )
  262.  
  263.         call flush_buffer
  264.  
  265.         call check_buffer
  266.         call check_carrier
  267.         if Carrier & BufferIsEmpty then
  268.             LoginOkay = check( User, Password )
  269.         else
  270.             do
  271.                 call log( 'HNDLELOGIN: !!WARNING!! Carrier lost or unexpected characters in input buffer')
  272.                 call log( 'HNDLELOGIN: !!WARNING!! Skipping user authentication' )
  273.             end
  274.  
  275.         Attempt = Attempt + 1
  276.  
  277.     end
  278.     
  279.     if LoginOkay = True then
  280.         do
  281.             call check_carrier
  282.             if Carrier then
  283.                 do
  284.                     /* Work-around for PPP routing bug */
  285.                     if ActiveUsers > 1 then do
  286.                         NetstatLOG = LogPath'\netstat'ModemIndex'.log'
  287.                         'netstat -r >'NetstatLog
  288.                         'start 'Path'\reestab.cmd 'ModemIndex
  289.                     end
  290.  
  291.                     call send( '$%' )
  292.                     call send( crlf )
  293.  
  294.                     /* built file name for user log */
  295.                     UserLog = LogPath'\'substr(date(european),4,2)||substr(date(european),7,2)'_'ModemIndex'.log'
  296.  
  297.                     call log('HNDLELOGIN: *** Startup PPP driver ***')
  298.                     StartTime = time(normal)
  299.                     StartDate = date(european)
  300.                     rc = time('R')
  301.  
  302.                     'PPP file' PPP_CFGFile
  303.  
  304.                     LoginTime = trunc(time('R'),0)
  305.  
  306.                     EndTime = time(normal)
  307.  
  308.                     call log('HNDLELOGIN: *** Shutdown PPP driver ***')
  309.  
  310.                     /* Log connection data */
  311.                     LogString = substr(User,1,20)||StartDate'   'StartTime'   'EndTime
  312.                     rc = lineout( UserLog, LogString )
  313.                     rc = lineout( UserLog )
  314.  
  315.                     rc = RxPrfAddLoginTime(User, LoginTime)
  316.  
  317.                     /* create flag file indicating that a user is active */
  318.                     call flagfile('LGOUT')
  319.  
  320.                     /* remove routing table entries like "xxx.yyy.zzz.11 xxx.yyy.zzz.11 ppp1" */
  321.                     Line = linein(PPP_CFGFile)
  322.                     do while(pos(":", Line) == 0)
  323.                         Line = linein(PPP_CFGFile)
  324.                     end
  325.                     rc = lineout(PPP_CFGFile)
  326.                     RemoteIP = substr(Line, pos(":", Line)+1)
  327.                     'route delete 'RemoteIP' 'RemoteIP
  328.  
  329.                 end
  330.         end
  331.     else
  332.         call log( 'HNDLELOGIN: Login failed. PPP was not started.' )
  333.  
  334.     return
  335.  
  336.  
  337.  
  338. /************************************************************************/
  339. /* welcome_screen ()                                                    */
  340. /*                                                                      */
  341. /* sends a text file (Welcom Screen) to modem user BEFORE user and      */
  342. /* password are requested                                               */
  343. /************************************************************************/
  344.  
  345. welcome_screen:
  346.  
  347.  
  348.     if length(stream(WelcomeFile, 'c', 'query exists')) > 0 then
  349.         do
  350.             Line = linein(WelcomeFile, 1, 1 )
  351.             call check_carrier    
  352.             LineCount = 0
  353.             do while (Line <> '$') & Carrier
  354.                 call check_carrier    
  355.                 if Carrier then
  356.                     do
  357.                         call send( Line )
  358.                         call send( crlf )
  359.                         LineCount = LineCount + 1
  360.                     end
  361.                 Line = linein(WelcomeFile)
  362.             end
  363.             rc = lineout(WelcomeFile)
  364.             if Carrier then
  365.                 call log('WELCSCREEN: Welcome screen ('LineCount' lines) sent')
  366.             else
  367.                 call log('WELCSCREEN: !!WARNING!! Welcome screen not (fully) sent')
  368.             
  369.         end
  370.  
  371.     return
  372.  
  373.  
  374.  
  375. /************************************************************************/
  376. /* get_user()                                                           */
  377. /*                                                                      */
  378. /* requests the username from modem user; there is a timeout after      */
  379. /* which the login procedure is canceled and the line is dropped        */
  380. /* (set in "Timeout"); there is also a maximum number of characters     */
  381. /* read from com port for username (set in "UserMaxLen")                */
  382. /************************************************************************/
  383.  
  384. get_user:
  385.  
  386.     call check_buffer
  387.     if BufferIsEmpty then
  388.         call send('   Login:')
  389.  
  390.     Dummy = 0
  391.     User = ''
  392.     UserLen = 0
  393.     Cycle = 0
  394.     UserTimeout = False
  395.     TooLong = False
  396.     Buffer = ''
  397.     do while (Buffer <> D2C(13)) & (UserTimeout == False) & (TooLong == False) & Carrier & BufferIsEmpty
  398.         call check_carrier
  399.         if Carrier then
  400.             do
  401.                 Buffer = ''
  402.                 rc = RxIOCtlReadChar( PortHandle, 1, 'Buffer' )
  403.                 select
  404.                     when rc = 0 then
  405.                         if UserLen < UserMaxLen then
  406.                             do
  407.                                 User = User||Buffer
  408.                                 UserLen = UserLen + 1
  409.                                 Cycle = 0
  410.                             end
  411.                         else
  412.                             TooLong = True
  413.                     when rc = -1 then                 /* send no echo for CR */
  414.                         Dummy = 1
  415.                     when rc = -2 then
  416.                         Cycle = Cycle + 1
  417.                 end
  418.                 MaxCycle = Timeout * 100 / ReadLim
  419.                 if Cycle > MaxCycle then
  420.                     UserTimeout = True
  421.  
  422.                 call check_carrier
  423.             end
  424.     end
  425.  
  426.     if Carrier & BufferIsEmpty then
  427.         call send( crlf )
  428.  
  429.     if UserTimeout = True then
  430.         do
  431.             call log( 'GETUSER   : !!WARNING!! Timeout waiting for username. Timout: 'Timeout's')
  432.             call check_carrier
  433.             if Carrier then
  434.                 call hangup
  435.         end
  436.     if TooLong = True then
  437.         do
  438.             call log( 'GETUSER   : !!WARNING!! Username length exceeded. Maximum length: 'UserMaxLen )
  439.             call check_carrier
  440.             if Carrier then
  441.                 call hangup
  442.         end
  443.     else
  444.         if Carrier then
  445.             call log( 'GETUSER   : User =' User )
  446.  
  447.     return User
  448.  
  449.  
  450.  
  451. /************************************************************************/
  452. /* get_password()                                                       */
  453. /*                                                                      */
  454. /* requests the password from modem user; there is a timeout after      */
  455. /* which the login procedure is canceled and the line is dropped        */
  456. /* (set in "Timeout"); there is also a maximum number of characters     */
  457. /* read from com port for username (set in "PwdMaxLen")                 */
  458. /************************************************************************/
  459.  
  460. get_password:
  461.  
  462.     call check_buffer
  463.     if BufferIsEmpty then
  464.         call send('Password:')
  465.  
  466.     Dummy = 0
  467.     Password = ''
  468.     PwdLen = 0
  469.     Cycle = 0
  470.     PwdTimeout = False
  471.     TooLong = False
  472.     Buffer = ''
  473.     do while (Buffer <> D2C(13)) & (PwdTimeout == False) & (TooLong == False) & Carrier & BufferIsEmpty
  474.         call check_carrier
  475.         if Carrier then
  476.             do
  477.                 Buffer = ''
  478.                 rc = RxIOCtlReadChar( PortHandle, -1, 'Buffer' )
  479.                 select
  480.                     when rc = 0 then
  481.                         if PwdLen < PwdMaxLen then
  482.                             do
  483.                                 Password = Password||Buffer
  484.                                 PwdLen = PwdLen + 1
  485.                                 Cycle = 0
  486.                             end
  487.                         else
  488.                             TooLong = True
  489.                     when rc = -1 then                 /* send no echo for CR */
  490.                         Dummy = 1
  491.                     when rc = -2 then
  492.                         Cycle = Cycle + 1
  493.                 end
  494.                 MaxCycle = Timeout * 100 / ReadLim
  495.                 if Cycle > MaxCycle then
  496.                     PwdTimeout = True
  497.  
  498.                 call check_carrier
  499.             end
  500.     end
  501.  
  502.     if Carrier & BufferIsEmpty then
  503.         call send( crlf )
  504.  
  505.     if PwdTimeout = True then
  506.         do
  507.             call log( 'GETPASSWD : !!WARNING!! Timeout waiting for password. Timeout: 'Timeout's' )
  508.             call check_carrier
  509.             if Carrier then
  510.                 call hangup
  511.         end
  512.     if TooLong = True then
  513.         do
  514.             call log( 'GETPASSWD : !!WARNING!! Password length exceeded. Maximum length: 'PwdMaxLen )
  515.             call check_carrier
  516.             if Carrier then
  517.                 call hangup
  518.         end
  519.     else
  520.         if Carrier then
  521.             call log( 'GETPASSWD : Password =' copies('*',PwdLen) )
  522.  
  523.     return Password
  524.  
  525.  
  526.  
  527. /************************************************************************/
  528. /* check(User,Password)                                                 */
  529. /*                                                                      */
  530. /* checks username and password given by modem user                     */
  531. /************************************************************************/
  532.  
  533. check:
  534.  
  535.     parse arg User, Password
  536.  
  537.     LoginOkay = RxPrfCheckPassword(User, Password)
  538.     if LoginOkay then
  539.         do
  540.              call log( 'CHECK     : User identification passed' )
  541.              call send( crlf )
  542.              call send('Login OK.')
  543.              call send( crlf )
  544.  
  545.              call send( crlf )
  546.              LastLogin = 'Last logout on 'RxPrfGetLastLoginDate(User)
  547.              call send(LastLogin)
  548.              call send( crlf )
  549.  
  550.              UserMail = MailPath'\'User'\*.msg'   
  551.              call SysFileTree UserMail, 'MailFiles', 'O'
  552.              call send( crlf )
  553.              if MailFiles.0 > 0 then
  554.                 call send('*** You have 'MailFiles.0' new mail(s) ***')
  555.              else
  556.                 call send('No new mail.')
  557.  
  558.              call send( crlf )
  559.              call send( crlf )
  560.  
  561.              UserRealName = RxPrfGetRealName(User)
  562.  
  563.             /* create flag file indicating that a user is active */
  564.             call flagfile('ACTIV')
  565.  
  566.             ActiveUsers = 0
  567.             do Index=1 to ModemNumber
  568.                 ActivFlagFile = Path'\L'Index'_ACTIV.FLG'
  569.                 if length(stream(ActivFlagFile, 'c', 'query exists')) > 0 then
  570.                     do
  571.                         ActiveUsers = ActiveUsers + 1
  572.                         Line = linein(ActivFlagFile)
  573.                         rc = lineout(ActivFlagFile)
  574.                     end
  575.                 else
  576.                     Line = 'not used'
  577.  
  578.                 call send( 'Modem 'Index' : 'Line )
  579.                 call send( crlf )
  580.             end
  581.             call send( crlf )
  582.         end
  583.     else
  584.         do
  585.             call log( 'CHECK     : !!WARNING!! User authentication failed' )
  586.             call send('Login failed.')
  587.             call send( crlf )
  588.  
  589.             FailLog = Path'\log\'substr(date(european),4,2)||substr(date(european),7,2)'_'ModemIndex'XX.LOG'
  590.             LogString = date(european)'   'time(normal)'   U='User'   P ='copies('*', length(Password))
  591.  
  592.             rc = lineout( FailLog, LogString )
  593.             rc = lineout( FailLog )
  594.  
  595.         end
  596.  
  597.     return LoginOkay
  598.  
  599.  
  600.  
  601. /************************************************************************/
  602. /* send ( sendstring )                                                  */
  603. /*                                                                      */
  604. /* send a character string off to the modem                             */
  605. /************************************************************************/
  606.  
  607. send:
  608.  
  609.     parse arg SendString
  610.  
  611.     Rem = 0
  612.     call check_carrier
  613.     if Carrier then
  614.         do
  615.             rc = RxIOCtlWrite( PortHandle, SendString, 'Rem' )
  616.             if rc <> 0 then
  617.                 say 'SEND      : ###ERROR### RxIOCtlWrite failed with rc='rc
  618.         end
  619.     else
  620.         do
  621.             if SendString == crlf then
  622.                 say 'SEND      : !!WARNING!! crlf not sent'
  623.             else
  624.                 say 'SEND      : !!WARNING!! 'SendString' not sent'
  625.  
  626.         end
  627.     return
  628.  
  629.  
  630.  
  631. /************************************************************************/
  632. /* log ( logstring )                                                    */
  633. /*                                                                      */
  634. /* logs the main activities of this REXX script                         */
  635. /************************************************************************/
  636.  
  637. log:
  638.  
  639.     parse arg LogString
  640.     LogString = strip(LogString,,D2C(13))
  641.  
  642.     rc = lineout( SystemLog, date(european)'-'time(normal)'' LogString )
  643.     if rc <> 0 then
  644.         say 'LOG       : ###ERROR### lineout failed with rc='rc
  645.  
  646.     say date(european)' 'time(normal)' 'left(LogString,61)
  647.  
  648.     return
  649.  
  650.  
  651.  
  652. /************************************************************************/
  653. /* flagfile ( Flag )                                                    */
  654. /*                                                                      */
  655. /* creates a flag file indicating current status of UserCtrl program    */
  656. /************************************************************************/
  657.  
  658. flagfile:
  659.  
  660.     parse arg Flag
  661.  
  662.     /* delete old flag file */
  663.     FlagFile = Path'\L'ModemIndex'_START.FLG'
  664.     if length(stream(FlagFile, 'c', 'query exists')) > 0 then
  665.         rc = SysFileDelete(FlagFile)
  666.     FlagFile = Path'\L'ModemIndex'_WAIT.FLG'
  667.     if length(stream(FlagFile, 'c', 'query exists')) > 0 then
  668.         rc = SysFileDelete(FlagFile)
  669.     FlagFile = Path'\L'ModemIndex'_LOGIN.FLG'
  670.     if length(stream(FlagFile, 'c', 'query exists')) > 0 then
  671.         rc = SysFileDelete(FlagFile)
  672.     FlagFile = Path'\L'ModemIndex'_ACTIV.FLG'
  673.     if length(stream(FlagFile, 'c', 'query exists')) > 0 then
  674.         rc = SysFileDelete(FlagFile)
  675.     FlagFile = Path'\L'ModemIndex'_LGOUT.FLG'
  676.     if length(stream(FlagFile, 'c', 'query exists')) > 0 then
  677.         rc = SysFileDelete(FlagFile)
  678.  
  679.  
  680.     FlagFile = Path'\L'ModemIndex'_'Flag'.FLG'
  681.     if Flag == "ACTIV" then
  682.         rc = lineout(FlagFile, UserRealName)
  683.  
  684.     rc = lineout(FlagFile)
  685.  
  686.     return
  687.  
  688.  
  689.  
  690. /************************************************************************/
  691. /* open_com_port()                                                      */
  692. /*                                                                      */
  693. /* opens the com port                                                   */
  694. /************************************************************************/
  695.  
  696.  
  697. open_com_port:
  698.  
  699.     rc = RxIOCtlOpenPort(PortName, 'PortHandle')
  700.     if rc <> 0 then
  701.         do
  702.             call log('COMOPEN   : ###ERROR### 'PortName' RxIOCtlOpenPort failed with rc='rc)
  703.             exit
  704.         end
  705.     else
  706.         call log('COMOPEN   : 'PortName' opened')
  707.  
  708.     return
  709.  
  710.  
  711.  
  712. /************************************************************************/
  713. /* close_com_port()                                                     */
  714. /*                                                                      */
  715. /* closes the com port                                                  */
  716. /************************************************************************/
  717.  
  718.  
  719. close_com_port:
  720.  
  721.     rc = RxIOCtlClosePort( PortHandle )
  722.     if rc <> 0 then
  723.         do
  724.             call log('COMCLOSE  : ###ERROR### 'PortName' RxIOCtlClosePort failed with rc='rc)
  725.             exit
  726.         end
  727.     else
  728.         call log('COMCLOSE  : 'PortName' closed')
  729.  
  730.     return
  731.  
  732.  
  733.  
  734. /************************************************************************/
  735. /* modem_init ()                                                        */
  736. /*                                                                      */
  737. /* initializes the modem; note that the modem should be set to NO       */
  738. /* auto answer (ATS0=0) since the script picks up the line if it        */
  739. /* detects a RING signal. Therefore the modem has to be set for         */
  740. /* echo. (ATE=1)                                                        */
  741. /************************************************************************/
  742.  
  743.  
  744. modem_init:
  745.  
  746.     call flush_buffer
  747.  
  748.     /* ATZ        Initialize modem */
  749.     /* Q0        Modem returns result code */
  750.     /* V1         Display result codes in verbose form */
  751.     /* S0=0        disable auto answer */
  752.     /* S3=13        Carriage return = ASCII 13 */
  753.     /* S4=10        Line feed = ASCII 10 */
  754.     /* S7=60    number of seconds the modem waits for a carrier */
  755.     /* S10=7    wait 7/10 seconds after loss of carrier before hangup */
  756.  
  757.     Rem = 0
  758.     rc = RxIOCtlWrite( PortHandle, 'ATZQ0V1S0=0S3=13S4=10S7=60S10=7'||D2C(13), 'Rem' )
  759.     if rc <> 0 then
  760.         call log('MODEM_INIT: ###MODEM ERROR### RxIOCtlWrite failed with rc='rc)
  761.     else
  762.         call log('MODEM_INIT: ATZ sent to modem')
  763.  
  764.     call SysSleep 1
  765.  
  766.     InitNotOK = True
  767.     do while InitNotOK
  768.         BufferEmpty = True
  769.         do while BufferEmpty
  770.             Buffer = ''
  771.             rc = RxIOCtlRead(PortHandle, 0, 1, 'Buffer')
  772.             if rc <> 0 then
  773.                 call log('MODEM_INIT: ###MODEM ERROR### RxIOCtlRead failed with rc='rc)
  774.             else
  775.                 if length(Buffer) > 3 then
  776.                     do
  777.                         BufferEmpty = False
  778.                     end
  779.         end
  780.         if Buffer == "OK"||crlf then
  781.             do
  782.                 call log('MODEM_INIT: OK received from modem')
  783.                 InitNotOK = False
  784.             end
  785.         else
  786.             call log('MODEM_INIT: ###MODEM ERROR### Expected:OK   Received:'Buffer)
  787.     end
  788.  
  789.     return
  790.  
  791.  
  792.  
  793. /************************************************************************/
  794. /* hangup()                                                             */
  795. /*                                                                      */
  796. /* forces the modem to drop the line                                    */
  797. /************************************************************************/
  798.  
  799. hangup:
  800.  
  801.     call flush_buffer
  802.  
  803.     Rem = 0
  804.     rc = RxIOCtlWrite(PortHandle, '+++', 'Rem')
  805.     if rc <> 0 then
  806.         call log('HANGUP    : ###ERROR### Could not send escape sequence (+++) to modem')
  807.     else
  808.         call log('HANGUP    : escape sequence (+++) sent to modem' )
  809.  
  810.  
  811.     call SysSleep 2
  812.  
  813.     Rem = 0
  814.     rc = RxIOCtlWrite(PortHandle, 'ATH0'||crlf, 'Rem')
  815.     if rc <> 0 then
  816.         call log('HANGUP    : ###ERROR### Could not send hangup command (ATH0) to modem')
  817.     else
  818.         call log('HANGUP    : hangup command (ATH0) sent to modem')
  819.  
  820.     call SysSleep 10
  821.  
  822.     return
  823.  
  824.  
  825. /************************************************************************/
  826. /* check_buffer ()                                                      */
  827. /*                                                                      */
  828. /* checks the input buffer of the com port for characters               */
  829. /************************************************************************/
  830.  
  831. check_buffer:
  832.  
  833.     /* there should be NO characters in the input buffer */
  834.     RxCount = 0
  835.     RxSize = 0
  836.     rc = RxIOCtlGetRxCount(PortHandle, 'RxCount', 'RxSize')
  837.     if RxCount > 0 then
  838.         do
  839.             BufferIsEmpty = False
  840.             call log( 'CHCKBUFFER: !!WARNING!! Unexpected characters in input buffer found')
  841.             call log( 'CHCKBUFFER: !!WARNING!! number of characters: 'RxCount )
  842.             call log( 'CHCKBUFFER: !!WARNING!! size of input buffer: 'RxSize )
  843.         end
  844.  
  845.     return
  846.  
  847.  
  848.  
  849. /************************************************************************/
  850. /* flush_buffer ()                                                      */
  851. /*                                                                      */
  852. /* flushes the input buffer                                             */
  853. /************************************************************************/
  854.  
  855. flush_buffer:
  856.  
  857.     rc = RxIOCtlFlushInput( PortHandle )
  858.  
  859.     call log( 'FLUSHBUFFR: Input buffer flushed' )
  860.  
  861.     BufferIsEmpty = True
  862.  
  863.     return
  864.  
  865.  
  866.  
  867. /************************************************************************/
  868. /* check_carrier ()                                                     */
  869. /*                                                                      */
  870. /* checks if the line is up and there is a carrier                      */
  871. /************************************************************************/
  872.  
  873. check_carrier:
  874.  
  875.     rc = RxIOCtlDetectCarrier( PortHandle )
  876.     if rc = 0 then
  877.         Carrier = True
  878.     else
  879.         do
  880.             Carrier = False
  881.             call log('CHKCARRIER: !!WARNING!! Carrier lost')
  882.         end
  883.  
  884.     return
  885.  
  886.  
  887.  
  888. /************************************************************************/
  889. /* error ()                                                             */
  890. /*                                                                      */
  891. /*                                                                      */
  892. /************************************************************************/
  893.  
  894. error:
  895.  
  896.     call log('ERROR     : ###ERROR### error() called')
  897.     call RxIOCtlClosePort PortHandle
  898.  
  899.     exit
  900.